package com.dcbeida.activity.stockdetail;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dcbeida.MyApplication;
import com.dcbeida.R;
import com.dcbeida.activity.BaseActivity;
import com.dcbeida.adapter.CyxqAdapter;
import com.dcbeida.bean.Ckxq;
import com.dcbeida.bean.CkxqHeadBean;
import com.dcbeida.bean.Drug;
import com.dcbeida.bean.DrugListBean;
import com.dcbeida.bean.Kcpdbg;
import com.dcbeida.widget.DataLoadDirection;
import com.dcbeida.widget.XListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 差异详情
 * Created by 150410 on 2017/10/26.
 */

public class CyxqActivity extends BaseActivity implements XListView.IXListViewListener, CyxqContract.View {
    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.rel_title1)
    RelativeLayout relTitle1;
    @Bind(R.id.tv_pdsj)
    TextView tvPdsj;
    @Bind(R.id.tv_pds)
    TextView tvPds;
    @Bind(R.id.tv_kcs)
    TextView tvKcs;
    @Bind(R.id.tv_pys)
    TextView tvPys;
    @Bind(R.id.tv_pks)
    TextView tvPks;
    @Bind(R.id.myList)
    XListView myList;
    private Kcpdbg.ListBean kcpdbg;
    private ArrayList<Object> datalist;
    private CyxqAdapter adapter;
    private CyxqPersentr mPresent;

    private boolean isEdit = false;
    private boolean isEnd = false;// 是否有更多
    private int Totle = 0;//
    private int pageindex = 0;
    private int pagesize = 10;
    int type = DataLoadDirection.Refresh;

    @Override
    protected int getLayoutResId() {
        return R.layout.cyxq_act;
    }

    @Override
    public void setData() {
        tvTitle.setText(R.string.pdcyxq);
        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            kcpdbg = (Kcpdbg.ListBean) bundle.getSerializable("Kcpdbg.ListBean");
            tvPdsj.setText(kcpdbg.getCreatetime());
            tvPds.setText(kcpdbg.getInventory());
            tvKcs.setText(kcpdbg.getStock());


            if ("".equalsIgnoreCase(kcpdbg.getOverage()) || kcpdbg.getOverage().equalsIgnoreCase("0")) {
                tvPys.setText("0");
            } else {
                tvPys.setText("+" + kcpdbg.getOverage());

            }

            if ("".equalsIgnoreCase(kcpdbg.getShortage()) || kcpdbg.getShortage().equalsIgnoreCase("0")) {
                tvPks.setText("0");
            } else {
                tvPks.setText("-" + kcpdbg.getShortage());
            }


        }

        datalist = new ArrayList<Object>();
        myList.setPullLoadEnable(false);
        myList.setPullRefreshEnable(true);
        myList.setXListViewListener(this);

        myList.setVerticalScrollBarEnabled(false);// 去掉竖向滚动条
        adapter = new CyxqAdapter(mContext);
        myList.setAdapter(adapter);

        mPresent = new CyxqPersentr(this, this);
        mPresent.getList(kcpdbg.getInventorylogid(), pageindex, pagesize);

    }


    @Override
    public void onRefresh() {
        // 执行刷新任务
        type = DataLoadDirection.Refresh;
        mPresent.getList(kcpdbg.getInventorylogid(), 0, pagesize);
    }

    @Override
    public void onLoadMore() {
        // 执行加载更多任务
        type = DataLoadDirection.LoadMore;
        if (isEnd) {
            mPresent.getList(kcpdbg.getInventorylogid(), pageindex + 1, pagesize);
        }
    }

    @Override
    public void showTelTip(String msg) {

    }

    @Override
    public void showView(boolean hasData) {

    }

    @Override
    public void showList(ArrayList<Ckxq.ListBean> list) {

        if (list.size() < 10)
            isEnd = false;
        else
            isEnd = true;

        Totle = adapter.getmArrayList().size() + list.size();


        if (type == DataLoadDirection.Refresh) {
            myList.stopRefresh();
            pageindex = 0;
            adapter.clearList();
        } else if (type == DataLoadDirection.LoadMore) {
            myList.stopLoadMore();
            pageindex++;
        }

        if (isEnd) {
            myList.showFooterView();
            myList.setPullLoadEnable(true);
        } else {
            myList.hideFooterView();
            myList.setPullLoadEnable(false);
        }

        HashMap<Integer, CkxqHeadBean> tagHashMap = adapter.getTagHashMap();

        int i = 0;
        for (Ckxq.ListBean listBean : list) {
            boolean isHave = false;
            for (Map.Entry<Integer, CkxqHeadBean> entry : tagHashMap.entrySet()) {
                Integer key = entry.getKey();
                CkxqHeadBean value = entry.getValue();
                if (listBean.getDrugid() == value.getDrugid()) {
                    isHave = true;
                    value.setInventory(value.getInventory() + Integer.parseInt(listBean.getInventory()));
                    value.setStock(value.getStock() + Integer.parseInt(listBean.getStock()));
                    //value.setOverage(value.getOverage()+Integer.parseInt(listBean.));

                }

            }
            if (!isHave) {
                CkxqHeadBean ckxqHeadBean = new CkxqHeadBean();
                ckxqHeadBean.setDrugid(listBean.getDrugid());

                DrugListBean drugBean = MyApplication.getInstance().getmDrugBean();
                String drugName = "";
                if (drugBean != null && drugBean.getList() != null) {
                    for (Drug drug : drugBean.getList()) {
                        if (listBean.getDrugid() == drug.getDrugid()) {
                            drugName = drug.getDrugname();
                        }
                    }
                }

                ckxqHeadBean.setDrugName(drugName);
                ckxqHeadBean.setInventory(Integer.parseInt(listBean.getInventory()));
                ckxqHeadBean.setStock(Integer.parseInt(listBean.getStock()));
                int pos = adapter.getmArrayList().size() + i;
                tagHashMap.put(pos, ckxqHeadBean);
            }
            i++;
        }

        adapter.setmArrayList(list);


    }


    @Override
    public void showData(Ckxq stockDetail) {

    }


    @OnClick(R.id.img_back)
    public void onClick() {
        finish();
    }

    @Override
    public void setPresenter(CyxqContract.Persent presenter) {

    }
}
